<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use app\admin\model\System;

/**
 * 通用上传接口
 * Class Upload
 * @package app\api\controller
 */
class Hsjson extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();

        if (!Session::has('admin_id')) {
            $result = [
                'error'   => 1,
                'message' => '未登录'
            ];
            echo json_encode($result);exit;
        }
    }

    /**
     * 通用文件上传接口
     * @return void
     */
    public function upload()
    {
        $file = $this->request->file('file');
        $maxWidth = input('max_width');     $maxHeight = input('max_height');   $path = input('path','images').'/';

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/'.$path);

            $save_path   = '/uploads/'.$path;
            $info        = $file->move($upload_path);//->validate(['size'=>104857600,'ext'=>'jpg,gif,png,jpeg,bmp'])//字节

            if ($info) {
                $url = str_replace('\\', '/', $save_path . $info->getSaveName());
                //判断上传文件是图片
                if(function_exists("exif_imagetype")){  //exif_imagetype函数快得多,需exif扩展支持，虚拟主机无法修改配置文件
                    $is_ok = exif_imagetype('.'.$url);
                }else{
                    $is_ok = getimagesize('.'.$url);//使用getimagesize代替
                }
                if($is_ok === false){
                    $result = ['error' => 0,'url' =>$url];  //非图片时直接返回，无需处理
                }else{
                    $result = resizeImage('.'.$url,$maxWidth,$maxHeight);
                }
            } else {
                $result = ['error' => 1, 'message' => $file->getError()];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }
    /**
     * 编辑器上传接口
     * @return void
     */
    public function uedit_upload()
    {
        $file = $this->request->file('imgFile');
        //各路径时允许的后缀名
        $ext_arr = [
            'image' => 'gif,jpg,jpeg,png,bmp',
            //'flash' => 'swf,flv',
            //'media' => 'swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb',
            'file' => 'pdf,doc,docx,xls,xlsx,ppt,txt,zip,rar,gz,7z,bz2',
        ];
        $dir_name = input('dir','image');

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/uedit/'.$dir_name.'/');
            $save_path   = '/uploads/uedit/'.$dir_name.'/';
            $info        = $file->validate(['ext'=>$ext_arr[$dir_name]])->move($upload_path);//->validate(['size'=>104857600,'ext'=>'jpg,gif,png,jpeg,bmp'])//字节

            if ($info) {
                $url = str_replace('\\', '/', $save_path . $info->getSaveName());
                //判断上传文件是图片
                if(function_exists("exif_imagetype")){  //exif_imagetype函数快得多,需exif扩展支持，虚拟主机无法修改配置文件
                    $is_ok = exif_imagetype('.'.$url);
                }else{
                    $is_ok = getimagesize('.'.$url);//使用getimagesize代替
                }
                if($is_ok === false){
                    $result = ['error' => 0,'url' =>$url];  //非图片时直接返回，无需处理
                }else{  //是图片时根据上传配置处理图片
                    $result = resizeImage('.'.$url);//编辑器中没有推荐尺寸，无需填写，默认为0
                }
            } else {
                $result = ['error' => 1, 'message' => $file->getError()];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }
    public function admin_img()
    {
        $config = [
            'size' => 100000,
        ];
        $file = $this->request->file('file');

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/images/');

            $save_path   = '/uploads/images/';
            $info        = $file->validate($config)->move($upload_path);

            if ($info) {    //上传成功后写入数据库
                $url = str_replace('\\', '/', $save_path . $info->getSaveName());
                if(db('admin_user')->where(['id'=>session('admin_id')])->update(['img'=>$url]) === false){
                    $result = ['error' => 1,'message' =>'请重试'];
                }else{
                    session('admin_img',$url);
                    $result = ['error' => 0,'url' =>$url];
                }
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $file->getError()
                ];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }


}